<template>
  <div class="mod-prod prod-list-mod">
    <!-- 查询和其他操作 -->
    <div class="search-bar">

      <el-form :inline="true" label-width="auto" class="search-form" size="small">

        <div class="input-row">

          <el-form-item label="生态圈名">
            <el-input
              clearable
              @keyup.enter.native="handleFind"
              class="filter-item"
              style="width: 200px;"
              v-model="keyword"
              placeholder="请输入生态圈名"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select
              v-model="ecosphereCode"
              filterable
              clearable
              remote
              reserve-keyword
              placeholder="请输入分类名"
              @keyup.enter.native="handleFind"
            >
              <el-option
                v-for="item in classValueList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <div class="default-btn primary-btn" @click="handleFind">{{$t('crud.searchBtn')}}</div>
          <div class="default-btn" @click="resetSearchForm()">{{$t('crud.reset')}}</div>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="operation-bar">
        <el-row :gutter="10" style="margin-bottom: 8px;">
          <div
            class="default-btn"
            @click="handleAdd">
            添加生态圈
          </div>
        </el-row>
      </div>

      <div class="table-con prod-table">
        <el-table :data="tableData"
                  style="width: 100%">

          <el-table-column label="序号" width="80" align="center">
            <template slot-scope="scope">
              <span>{{scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="分类" width="100" align="center">
            <template slot-scope="scope">
              <el-tag v-for="item in classValueList" :key="item.code" v-if="scope.row.ecosphereCode == item.code"
                      :type="item.listClass">{{item.name}}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="生态圈名称" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="Logo地址" width="500" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.pic }}</span>
            </template>
          </el-table-column>
          <el-table-column label="生态圈地址" width="300" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.url }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" width="100" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.status == 0">
                <span>已启用</span>
              </template>
              <template v-if="scope.row.status == 1">
                <span>已删除</span>
              </template>
              <template v-if="scope.row.status == 2">
                <span>已停用</span>
              </template>
            </template>
          </el-table-column>

          <el-table-column align="center" label="是否艺术交易">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.ageCheck"
                active-value="1"
                inactive-value="0"
                active-color="#1890FF"
                inactive-color="#A9A8A8"
                @change="turn(scope.row)"
              />
            </template>
          </el-table-column>

          <el-table-column label="生态圈备注" width="200" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.remark }}</span>
            </template>
          </el-table-column>
          <el-table-column label="排序" width="60" align="center" prop="sort">
            <template slot-scope="scope">
              <el-tag type="warning">{{ scope.row.sort ||0}}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" width="160" align="center" prop="createTime" s>
            <template slot-scope="scope">
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>



          <el-table-column label="操作" fixed="right" min-width="230">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" v-if="scope.row.status != 1" type="primary" size="small">编辑
              </el-button>
              <el-button @click="handleOpen(scope.row)" v-if="scope.row.status == 2" type="success" size="small">启动
              </el-button>
              <el-button @click="handleClose(scope.row)" v-if="scope.row.status == 0" type="warning" size="small">停用
              </el-button>
              <el-button @click="handleDelete(scope.row)" type="danger" size="small"  v-if="scope.row.status != 1" >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="block">
      <el-pagination
        v-if="tableData.length"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类" :label-width="formLabelWidth" prop="ecosphereCode">
          <el-select
            v-model="form.ecosphereCode"
            size="small"
            placeholder="请选择"
            style="width:640px"
          >
            <el-option
              v-for="item in classValueList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
          <el-radio-group v-model="form.type" >
            <el-radio :label=1>微发行</el-radio>
            <el-radio :label=2>外链</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生态圈名称" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.name" auto-complete="off" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="Logo地址" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.pic" auto-complete="off" maxlength="500"></el-input>
        </el-form-item>
        <el-form-item label="生态圈地址" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.url" auto-complete="off" maxlength="500"></el-input>
        </el-form-item>
        <el-form-item label="生态圈备注" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.remark" auto-complete="off" maxlength="20"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        classValueList: [],
        tableData: [],
        keyword: '',
        ecosphereCode: '',
        currentPage: 1,
        pageSize: 10,
        total: 0, // 总数量
        title: '增加生态圈',
        dialogFormVisible: false, // 控制弹出框
        formLabelWidth: '120px',
        isEditForm: false,
        form: {
          name: '',
          type: 1
        },
        rules: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在1到20个字符'},
          ],
          ecosphereCode: [
            {required: true, message: '分类名称不能为空', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在1到20个字符'},
          ],
          sort: [
            {required: false, message: '排序字段不能为空', trigger: 'blur'},
            {pattern: /^[0-9]\d*$/, message: '排序字段只能为自然数'}
          ]
        }
      }
    },
    created () {
      this.tagList()
      this.classList()
    },
    methods: {
      classList: function () {
        this.$http({
          url: this.$http.adornUrl('/admin/ecosphere/ecosphereClassification/getAllEcosphereClassification'),
          method: 'get'
        }).then(({data}) => {
          this.classValueList = data
        })
      },
      tagList: function () {
        var params = {}
        params.name = this.keyword
        params.ecosphereCode = this.ecosphereCode
        params.pageIndex = this.currentPage
        params.pageSize = this.pageSize
        this.$http({
          url: this.$http.adornUrl('/admin/ecosphere/pageEcosphere'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: this.currentPage,
                size: this.pageSize
              },
              params
            )
          ),
        }).then(({data}) => {
          this.tableData = data.records
          this.currentPage = data.current
          this.pageSize = data.size
          this.total = data.total
        })
      },
      getFormObject: function () {
        var formObject = {
          id: null,
          name: null,
          type: 1,
          ecosphereCode: null,
          sort: this.tableData.length + 1
        }
        return formObject
      },

      // 搜索
      handleFind: function () {
        this.currentPage = 1
        this.tagList()
      },

      // 重置
      resetSearchForm () {
        this.keyword = ''
        this.ecosphereCode = ''

        this.currentPage = 1
        this.tagList() // 重置表单加载初始数据
      },

      handleAdd: function () {
        this.title = '增加生态圈'
        this.dialogFormVisible = true
        this.form = this.getFormObject()
        this.isEditForm = false
      },
      handleEdit: function (row) {
        this.title = '编辑生态圈'
        this.dialogFormVisible = true
        this.isEditForm = true
        this.form = JSON.parse(JSON.stringify(row))
      },
      turn: function (row) {
        var that = this
        that.$http({
          url: that.$http.adornUrl(`/admin/ecosphere/updateEcosphereOneById`),
          method: 'put',
          data: this.$http.adornData(
            row)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.tagList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      handleOpen: function (row) {
        var that = this
        row.status = 0
        that.$http({
          url: that.$http.adornUrl(`/admin/ecosphere/updateEcosphereOneById`),
          method: 'put',
          data: this.$http.adornData(
            row)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.tagList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      handleClose: function (row) {
        var that = this
        row.status = 2
        that.$http({
          url: that.$http.adornUrl(`/admin/ecosphere/updateEcosphereOneById`),
          method: 'put',
          data: this.$http.adornData(
            row)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.tagList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      handleDelete: function (row) {
        var that = this
        this.$confirm('此操作将把生态圈删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            that.delete(row)
          })
          .catch(() => {
            this.$commonUtil.message.info('已取消删除')
          })
      },
      delete: function (row) {
        var that = this
        // var ids = list.map(item => {
        //   return item.id
        // })
        row.status = 1,
        that.$http({
          url: that.$http.adornUrl(`/admin/ecosphere/updateEcosphereOneById`),
          method: 'put',
          data: this.$http.adornData(
            row)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.tagList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      handleCurrentChange: function (val) {
        this.currentPage = val
        this.tagList()
      },
      submitForm: function () {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            console.log('校验失败')
            return
          } else {
            if (this.isEditForm) {
              this.$http({
                url: this.$http.adornUrl('/admin/ecosphere/updateEcosphereOneById'),
                method: 'put',
                data: this.$http.adornData(
                  this.form)
              }).then(({data}) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.tagList()
                  }
                })
              })
            } else {
              this.$http({
                url: this.$http.adornUrl('/admin/ecosphere/saveEcosphere'),
                method: 'post',
                data: this.$http.adornData(
                  this.form)
              }).then(({data}) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.tagList()
                  }
                })
              })
            }
          }
        })
      }
    }
  }
</script>
